<?php
class ManageAction extends AppAction
{
    private $lib;
    private $adminGrand;
    function __construct ()
    {
        parent::__construct();
        require_once APP_LIB_PATH . 'adminLib.php';
        $this->lib = new AdminLib();
        AdminVisitor::checkGrant('manage');
    }
    
	public function updateAllFullTimeStudentGrade()
	{
		require_once APP_LIB_PATH . 'fullTimeLib.php';
		$fullTimeLib = new FullTimeLib ();
		$studentInfo = $fullTimeLib->getStudentList ();
		if (! $studentInfo)
		{
			BaseAction::adminError( "没有需要更新的学生" );
		}
		foreach ( $studentInfo as $key => $value )
		{
			$info->GradeId = $value ['GradeId'] + 1;
			$fullTimeLib->setInfoById ( $info, $value ['Id'] );
		}
		BaseAction::adminSuccess( "更新成功" );
	}
	
	public function updateAllLotusStudentGrade()
	{
		require_once APP_LIB_PATH . 'lotusLib.php';
		$lotusLib = new LotusLib();
		$studentInfo = $lotusLib->getStudentList ();
		if (! $studentInfo)
		{
			BaseAction::adminError ( "没有需要更新的学生" );
		}
		foreach ( $studentInfo as $key => $value )
		{
			$info->GradeId = $value ['GradeId'] + 1;
			$lotusLib->setInfoById ( $info, $value ['Id'] );
		}
		BaseAction::adminSuccess ( "更新成功" );
	}
    /**
     * 管理员权限列表
     * 管理员列表
     * 添加管理员
     * 编辑管理员（修改密码）
     * 权限设置
     * 删除管理员
     */
    public function adminlist ()
    {
        ViewData::$form_data->list = $this->lib->getList();
        BaseAction::loadView("admin/adminList");
    }
    public function adminadd ()
    {
        ViewData::$form_data = new E_administrate();
        ViewData::$form_hint = '';
        if ($_POST) {
            try {
                $this->adminAddForm();
                if ($this->adminAddLogic()) {
                    ViewData::$form_hint = "添加成功";
                    Header ( "Location:  " . siteUrl ( 'manage/adminlist' ) . "" );
                } else {
                    ViewData::$form_hint = "添加失败，请检查您所填写的用户名是否已经填写过";
                }
            } catch (Exception $e) {
                ViewData::$form_hint = $e->getMessage();
            }
        }
        $this->getGrant();
        BaseAction::loadView("admin/adminAdd");
    }
    private function adminAddForm ()
    {
        ViewData::$form_data = BaseAction::parseForm("POST", 
        ViewData::$form_data);
        BaseAction::checkForm('AdminName', 'POST', '管理员用户名不能为空', 
        array(2, 2, 64, "用户名长度错误"), FALSE, FALSE);
        BaseAction::checkForm('TrueName', 'POST', '真实姓名不能为空', 
        array(2, 2, 64, "真实姓名长度错误"), FALSE, FALSE);
        BaseAction::checkForm('Password', 'POST', '密码不能为空', 
        array(2, 6, 16, "密码长度错误"), FALSE, FALSE);
        return;
    }
    private function adminAddLogic ()
    {
        $grant = array();
        if (isset($_POST['camp'])) {
            $grant['camp'] = $_POST['camp'];
        }
        if (isset($_POST['fullTime'])) {
            $grant['fullTime'] = $_POST['fullTime'];
        }
        if (isset($_POST['lotus'])) {
            $grant['lotus'] = $_POST['lotus'];
        }
        if (isset($_POST['finance'])) {
            $grant['finance'] = 1;
        }
        if (isset($_POST['manage'])) {
            $grant['manage'] = 1;
        }
        ViewData::$form_data->AdminGrant = json_encode($grant);
        return $this->lib->addInfo(ViewData::$form_data);
    }
    public function adminedit ()
    {
        if (! isset($_GET['id'])) {
            BaseAction::adminError("对不起，请不要非法访问");
        }
        $info = $this->lib->getById($_GET['id']);
        $info->AdminGrant = json_decode($info->AdminGrant,TRUE);
        ViewData::$form_data = $info;
        ViewData::$form_hint = '';
        if ($_POST) {
            try {
                $this->adminEditForm();
                if ($this->adminEditLogic()) {
                    BaseAction::adminSuccess("修改成功", 
                    siteUrl('manage/adminlist'));
                } else {
                    ViewData::$form_hint = "修改失败，请检查您所填写的用户名是否已经填写过";
                }
            } catch (Exception $e) {
                ViewData::$form_hint = $e->getMessage();
            }
        }
        $this->getGrant();
        BaseAction::loadView("admin/adminEdit");
    }
    private function adminEditForm ()
    {
        ViewData::$form_data = BaseAction::parseForm("POST", 
        ViewData::$form_data);
        BaseAction::checkForm('TrueName', 'POST', '真实姓名不能为空', 
        array(2, 2, 64, "真实姓名长度错误"), FALSE, FALSE);
        BaseAction::checkForm('Password', 'POST#', '密码不能为空', 
        array(2, 6, 16, "密码长度错误"), FALSE, FALSE);
        return;
    }
    private function adminEditLogic ()
    {
        $grant = array();
        if (isset($_POST['camp'])) {
            $grant['camp'] = $_POST['camp'];
        }
        if (isset($_POST['fullTime'])) {
            $grant['fullTime'] = $_POST['fullTime'];
        }
        if (isset($_POST['lotus'])) {
            $grant['lotus'] = $_POST['lotus'];
        }
        if (isset($_POST['finance'])) {
            $grant['finance'] = 1;
        }
        if (isset($_POST['manage'])) {
            $grant['manage'] = 1;
        }
    	if (isset($_POST['teacher'])) {
            $grant['teacher'] = 1;
        }
        ViewData::$form_data->AdminGrant = json_encode($grant);
        return $this->lib->editInfoByAdminName(ViewData::$form_data);
    }
    private function getGrant ()
    {
        $adminGrant = array();
        require_once APP_LIB_PATH . 'campLib.php';
        require_once APP_LIB_PATH . 'fullTimeLib.php';
        require_once APP_LIB_PATH . 'lotusLib.php';
        $camplib = new CampLib();
        $fullTimeLib = new FullTimeLib();
        $lotusLib = new LotusLib();
        ViewData::$form_data->camp['summer'] = $camplib->getGradeList(1);
        ViewData::$form_data->camp['winter'] = $camplib->getGradeList(2);
        ViewData::$form_data->fullTime = $fullTimeLib->getGradeList();
        ViewData::$form_data->lotus = $lotusLib->getGradeList();
    }
    public function admindel ()
    {
        if (! isset($_GET['id'])) {
            BaseAction::adminError("对不起，请不要非法访问");
        }
        $this->lib->delAdminById($_GET['id']);
        BaseAction::adminSuccess("删除成功");
    }
}